package com.njit.office.controller;

import com.njit.office.form.AddInformForm;
import com.njit.office.model.InformModel;
import com.njit.office.model.InformState;
import com.njit.office.model.Result;
import com.njit.office.service.InformService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/informs/")
public class InformController {

    @Resource
    InformService informService;

    @PostMapping("add")
    @ResponseBody
    public Result createInform(@ModelAttribute("addInformForm")AddInformForm form){
        return informService.createInform(form);
    }

    @GetMapping("update/read/{userInformId}")
    @ResponseBody
    public Result readInform(@PathVariable("userInformId") String userInformId) {

        return informService.updateUserInformsState(userInformId, InformState.FINISHED);
    }

    @GetMapping("update/todo/{userInformId}")
    @ResponseBody
    public Result toDoInform(@PathVariable("userInformId") String userInformId) {

        return informService.updateUserInformsState(userInformId, InformState.UNDO);
    }
    @GetMapping("update/do/{userInformId}")
    @ResponseBody
    public Result doInform(@PathVariable("userInformId") String userInformId) {

        return informService.updateUserInformsState(userInformId, InformState.FINISHED);
    }
    @GetMapping("update/del/{userInformId}")
    @ResponseBody
    public Result delInform(@PathVariable("userInformId") String userInformId) {

        return informService.del(userInformId);
    }

    @GetMapping("updateView/unread")
    public String updateUnRead(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("userId") == null) {
            return "inform::unReadFragment";
        }
        String userId = (String) session.getAttribute("userId");
        Result res1 = informService.getUnReadInform(userId);
        model.addAttribute("unReadInforms", sortInforms((List<InformModel>) res1.getData()));
        return "inform::unReadFragment";
    }

    @GetMapping("updateView/undo")
    public String updateUnDo(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("userId") == null) {
            return "inform::unDoFragment";
        }
        String userId = (String) session.getAttribute("userId");
        Result res1 = informService.getUnDoInform(userId);
        model.addAttribute("unDoInforms", sortInforms((List<InformModel>) res1.getData()));
        return "inform::unDoFragment";
    }

    @GetMapping("updateView/finished")
    public String updateFinished(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("userId") == null) {
            return "inform::finishedFragment";
        }
        String userId = (String) session.getAttribute("userId");
        Result res1 = informService.getFinishedInform(userId);
        model.addAttribute("finishedInforms", sortInforms((List<InformModel>) res1.getData()));
        return "inform::finishedFragment";
    }





    public static List<InformModel> sortInforms(List<InformModel> informModels) {
        Collections.sort(informModels, new Comparator<InformModel>() {
            @Override
            public int compare(InformModel o1, InformModel o2) {
                long l = o1.getCreateTime().getTime() - o2.getCreateTime().getTime();
                if (l == 0) {
                    return 0;
                }
                if (l > 0) {
                    return -1;
                }
                return 1;
            }
        });
        return informModels;
    }
}
